#open BUFF,"< /data2/external_data/Chien_Jeremy_jrc13/s110998.cnv_sv_tumor_normal/other/snv.mix/normal.snvs.raw" or die "can't open normal.snvs.raw";
#open WRBUFF,">normal.output";
$file = $ARGV[0];
chomp($file);
$outfile = $ARGV[1];
chomp($outfile);
open BUFF,"<$file" or die "can't open $file";
open WRBUFF,">$outfile";
open WRB,">details_$outfile";
$cov_total = 0;
for($i=0;$i<=40;$i=$i+5)
{
	$wbf = "WRBUFF$i";
	$file_out = "cov$i"."_$outfile";
	open($wbf,">$file_out");
	${"cov$i"}= 0;
	${"cov_num_all$i"}= 0;
	${"cov_num_all_qual20_$i"}= 0;
	${"cov_num_all_qual25_$i"}= 0;
	${"cov_num_all_qual30_$i"}= 0;
}
while($line = <BUFF>)
{
	chomp($line);
	$cov_total++;
	@array = split(/\t/,$line);
	@arr = split(',',$array[3]);
	pop(@arr);
	$num_ref = shift(@arr);
	$num_alt = shift(@arr);
	if($array[2] ne "N")
        {
		@arr1 = sort {$a <=> $b} @arr;
		if($arr1[2] == 1)
		{
			$val = 110;
		}
		else
		{
			$val = -10*(log(1-$arr1[2])/log(10));
		}
		print WRBUFF $val."\n";
	}
	$num_ref =~ s/^\w://g;
        $num_alt =~ s/^\w://g;
        $cov = $num_ref+$num_alt;
	#print $cov."\n";
        #$m = 0;
        #for($i=40;$m ==0;$i=$i-5)
	$i=40;
	while($i >= 0)
        {
		#print "$cov\t$i\n";
                if($cov >= $i)
                {
                        $wbf = "WRBUFF$i";
                        print $wbf "$cov\n";
			if($array[2] ne "N")
			{
				${"cov_num_all$i"}++;
			}
			if($array[2] ne "N" && $val > 30)
			{
				${"cov_num_all_qual30_$i"}++;	
			}
			if($array[2] ne "N" && $val > 25)
			{
                                ${"cov_num_all_qual25_$i"}++;
                        }
			if($array[2] ne "N" && $val > 20)
                        {
                                ${"cov_num_all_qual20_$i"}++;
                        }
                        ${"cov$i"}++;

         #               $m =1;
                }
		$i=$i-5;
        }
}
for($i=0;$i<=40;$i=$i+5)
{
	print WRB "coverage > $i ".${"cov$i"}."\n";
	print WRB "coverage > $i && number of alleles 2 ".${"cov_num_all$i"}."\n";
	print WRB "coverage > $i && number of alleles 2 && qual > 20 ".${"cov_num_all_qual20_$i"}."\n";
	print WRB "coverage > $i && number of alleles 2 && qual > 25 ".${"cov_num_all_qual25_$i"}."\n";
	print WRB "coverage > $i && number of alleles 2 && qual > 30 ".${"cov_num_all_qual30_$i"}."\n";
}
print WRB "total coverage $cov_total\n";

